﻿
namespace UserDefinedForms.Windows.Forms
{
	/// <summary>
	/// abstract ControlListItem typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class ControlListItemDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<ControlListItemDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.ControlListItem _currentControlListItem = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxControlListItemDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxControlListItemErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxControlListItemBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the FormListId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxFormListIdDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the ControlId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxControlIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the ItemData property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxItemDataDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Status property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxStatusDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.FormControl> _ControlIdList;
		
		/// <summary> 
		/// The list of selectable FormControl
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.FormControl> ControlIdList
		{
			get {return this._ControlIdList;}
			set 
			{
				this._ControlIdList = value;
				this.uxControlIdDataGridViewColumn.DataSource = null;
				this.uxControlIdDataGridViewColumn.DataSource = this._ControlIdList;
			}
		}
		
		private bool _allowNewItemInControlIdList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of FormControl
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInControlIdList
		{
			get { return _allowNewItemInControlIdList;}
			set
			{
				this._allowNewItemInControlIdList = value;
				this.uxControlIdDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.ControlListItem> _ControlListItemList;
				
		/// <summary> 
		/// The list of ControlListItem to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.ControlListItem> ControlListItemList
		{
			get {return this._ControlListItemList;}
			set
			{
				this._ControlListItemList = value;
				this.uxControlListItemBindingSource.DataSource = null;
				this.uxControlListItemBindingSource.DataSource = value;
				this.uxControlListItemDataGridView.DataSource = null;
				this.uxControlListItemDataGridView.DataSource = this.uxControlListItemBindingSource;				
				//this.uxControlListItemBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxControlListItemBindingSource_ListChanged);
				this.uxControlListItemBindingSource.CurrentItemChanged += new System.EventHandler(OnControlListItemBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnControlListItemBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentControlListItem = uxControlListItemBindingSource.Current as Entities.ControlListItem;
			
			if (_currentControlListItem != null)
			{
				_currentControlListItem.Validate();
			}
			//_ControlListItem.Validate();
			OnCurrentEntityChanged();
		}

		//void uxControlListItemBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <see cref="Entities.ControlListItem"/> instance.
		/// </summary>
		public Entities.ControlListItem SelectedControlListItem
		{
			get {return this._currentControlListItem;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxControlListItemDataGridView.VirtualMode;}
			set
			{
				this.uxControlListItemDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxControlListItemDataGridView.AllowUserToAddRows;}
			set {this.uxControlListItemDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxControlListItemDataGridView.AllowUserToDeleteRows;}
			set {this.uxControlListItemDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <see cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxControlListItemDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxControlListItemDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="ControlListItemDataGridViewBase"/> class.
		/// </summary>
		public ControlListItemDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxControlListItemDataGridView = new System.Windows.Forms.DataGridView();
			this.uxControlListItemBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxControlListItemErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxFormListIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxControlIdDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxItemDataDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxStatusDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxControlIdBindingSource = new FormControlBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxControlIdBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlListItemDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlListItemBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlListItemErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxControlListItemErrorProvider
			// 
			this.uxControlListItemErrorProvider.ContainerControl = this;
			this.uxControlListItemErrorProvider.DataSource = this.uxControlListItemBindingSource;						
			// 
			// uxControlListItemDataGridView
			// 
			this.uxControlListItemDataGridView.AutoGenerateColumns = false;
			this.uxControlListItemDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxControlListItemDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxFormListIdDataGridViewColumn,
		this.uxControlIdDataGridViewColumn,
		this.uxItemDataDataGridViewColumn,
		this.uxStatusDataGridViewColumn			});
			this.uxControlListItemDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxControlListItemDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxControlListItemDataGridView.Name = "uxControlListItemDataGridView";
			this.uxControlListItemDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxControlListItemDataGridView.TabIndex = 0;	
			this.uxControlListItemDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxControlListItemDataGridView.EnableHeadersVisualStyles = false;
			this.uxControlListItemDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnControlListItemDataGridViewDataError);
			this.uxControlListItemDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnControlListItemDataGridViewCellValueNeeded);
			this.uxControlListItemDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnControlListItemDataGridViewCellValuePushed);
			
			//
			// uxFormListIdDataGridViewColumn
			//
			this.uxFormListIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxFormListIdDataGridViewColumn.DataPropertyName = "FormListId";
			this.uxFormListIdDataGridViewColumn.HeaderText = "FormListId";
			this.uxFormListIdDataGridViewColumn.Name = "uxFormListIdDataGridViewColumn";
			this.uxFormListIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxFormListIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxFormListIdDataGridViewColumn.ReadOnly = true;		
			//
			// uxControlIdDataGridViewColumn
			//
			this.uxControlIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxControlIdDataGridViewColumn.DataPropertyName = "ControlId";
			this.uxControlIdDataGridViewColumn.HeaderText = "ControlId";
			this.uxControlIdDataGridViewColumn.Name = "uxControlIdDataGridViewColumn";
			this.uxControlIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxControlIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxControlIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxItemDataDataGridViewColumn
			//
			this.uxItemDataDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxItemDataDataGridViewColumn.DataPropertyName = "ItemData";
			this.uxItemDataDataGridViewColumn.HeaderText = "ItemData";
			this.uxItemDataDataGridViewColumn.Name = "uxItemDataDataGridViewColumn";
			this.uxItemDataDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxItemDataDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxItemDataDataGridViewColumn.ReadOnly = false;		
			//
			// uxStatusDataGridViewColumn
			//
			this.uxStatusDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxStatusDataGridViewColumn.DataPropertyName = "Status";
			this.uxStatusDataGridViewColumn.HeaderText = "Status";
			this.uxStatusDataGridViewColumn.Name = "uxStatusDataGridViewColumn";
			this.uxStatusDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxStatusDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxStatusDataGridViewColumn.ReadOnly = false;		
			//
			// uxControlIdDataGridViewColumn
			//				
			this.uxControlIdDataGridViewColumn.DisplayMember = "Name";	
			this.uxControlIdDataGridViewColumn.ValueMember = "ControlId";	
			this.uxControlIdDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxControlIdDataGridViewColumn.DataSource = uxControlIdBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxControlListItemDataGridView);
			this.Name = "ControlListItemDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxControlIdBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlListItemErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlListItemDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlListItemBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				ControlListItemDataGridViewEventArgs args = new ControlListItemDataGridViewEventArgs();
				args.ControlListItem = _currentControlListItem;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class ControlListItemDataGridViewEventArgs : System.EventArgs
		{
			private Entities.ControlListItem	_ControlListItem;
	
			/// <summary>
			/// the  Entities.ControlListItem instance.
			/// </summary>
			public Entities.ControlListItem ControlListItem
			{
				get { return _ControlListItem; }
				set { _ControlListItem = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxControlListItemDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnControlListItemDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxControlListItemDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnControlListItemDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxControlListItemDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxFormListIdDataGridViewColumn":
						e.Value = ControlListItemList[e.RowIndex].FormListId;
						break;
					case "uxControlIdDataGridViewColumn":
						e.Value = ControlListItemList[e.RowIndex].ControlId;
						break;
					case "uxItemDataDataGridViewColumn":
						e.Value = ControlListItemList[e.RowIndex].ItemData;
						break;
					case "uxStatusDataGridViewColumn":
						e.Value = ControlListItemList[e.RowIndex].Status;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxControlListItemDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnControlListItemDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxControlListItemDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxFormListIdDataGridViewColumn":
						ControlListItemList[e.RowIndex].FormListId = (System.Int32)e.Value;
						break;
					case "uxControlIdDataGridViewColumn":
						ControlListItemList[e.RowIndex].ControlId = (System.Int32)e.Value;
						break;
					case "uxItemDataDataGridViewColumn":
						ControlListItemList[e.RowIndex].ItemData = (System.String)e.Value;
						break;
					case "uxStatusDataGridViewColumn":
						ControlListItemList[e.RowIndex].Status = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
